﻿#region Usings

using System.Windows;
using Box2DX.Dynamics;
using SilverBox.Physics;

#endregion

namespace SilverBox.Behaviors
{
    public class BodyBehavior : PhysicsBehavior<FrameworkElement>
    {
        private BodyParameters bodyParameters;
        private Entity entity;

        public BodyBehavior()
        {
            bodyParameters = new BodyParameters();
        }

        private Body Body
        {
            get { return entity.Body; }
        }

        public Entity Entity
        {
            get { return entity; }
        }

        public bool IsStatic
        {
            get { return BodyParameters.IsStatic; }
            set { BodyParameters.IsStatic = value; }
        }

        public double Density
        {
            get { return BodyParameters.Density; }
            set { BodyParameters.Density = value; }
        }

        public double Friction
        {
            get { return BodyParameters.Friction; }
            set { BodyParameters.Friction = value; }
        }

        public double Restitution
        {
            get { return BodyParameters.Restitution; }
            set { BodyParameters.Restitution = value; }
        }

        public Vector InitialVelocity
        {
            get { return BodyParameters.InitialVelocity; }
            set { BodyParameters.InitialVelocity = value; }
        }

        public double InitialRotationalVelocity
        {
            get { return BodyParameters.InitialRotationalVelocity; }
            set { BodyParameters.InitialRotationalVelocity = value; }
        }

        public bool CanRotate
        {
            get { return BodyParameters.CanRotate; }
            set { BodyParameters.CanRotate = value; }
        }

        public double LinearDamping
        {
            get { return BodyParameters.LinearDamping; }
            set { BodyParameters.LinearDamping = value; }
        }

        public double AngularDamping
        {
            get { return BodyParameters.AngularDamping; }
            set { BodyParameters.AngularDamping = value; }
        }

        private BodyParameters BodyParameters
        {
            get { return bodyParameters; }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            entity = new Entity(AssociatedObject);

            AssociatedObject.Loaded += newElement_Loaded;
        }


        private void newElement_Loaded(object sender, RoutedEventArgs e)
        {
            AssociatedObject.Loaded -= newElement_Loaded;

            Simulator simulator = Simulator;

            if (simulator == null)
            {
                Dispatcher.BeginInvoke(delegate
                                           {
                                               Simulator.AddEntity(entity, BodyParameters);

                                               entity.Body.AllowSleeping = true;
                                           });
            }
            else
            {
                simulator.AddEntity(entity, BodyParameters);
                entity.Body.AllowSleeping = true;
                ;
            }
        }
    }
}